package com.wenhai.spring.resource;

import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.EncodedResource;

import javax.annotation.PostConstruct;
import java.io.IOException;

/**
 * 注入{@link org.springframework.core.io.Resource}
 *
 * @author wenhai
 * @date   2021/5/1
 * @see org.springframework.core.io.Resource
 * @see org.springframework.beans.factory.annotation.Value
 * @see org.springframework.beans.factory.annotation.Autowired
 * @see org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor
 */
public class InjectingResourceDemo {
    @Value("classpath:/META-INF/default.properties")
    private Resource defaultPropertiesResource;

    @PostConstruct
    public void init() throws IOException {
        EncodedResource encodedResource = new EncodedResource(defaultPropertiesResource, "UTF-8");
        System.out.println(IOUtils.toString(encodedResource.getReader()));
    }


    public static void main(String[] args) {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
        context.register(InjectingResourceDemo.class);
        context.refresh();
        context.close();

    }
}
